package rmi;


import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;

import data.datafactory.DatabaseFactoryMySqlImpl;
import dataservice.chooseCourseDataService.ChooseCourseDatabaseService;
import dataservice.courseDataService.CourseDatabaseService;
import dataservice.datafactoryservice.DatabaseFactory;
import dataservice.frameworkDataService.FrameworkDatabaseService;
import dataservice.informDataService.InformDataService;
import dataservice.teachPlanDataService.TeachPlanDatabaseService;
import dataservice.userDataService.UserDatabaseService;

public class RMI {
//	private static final String ADDRESS_STRING="114.212.43.162";
	private static final String ADDRESS_STRING="localhost";
	

	public void startRMI() {
		DatabaseFactory databaseFactory=DatabaseFactoryMySqlImpl.getInstance();
		try {
			LocateRegistry.createRegistry(8888);  
	        TeachPlanDatabaseService teachPlanData=databaseFactory.getTeachPlanDatabaseService();  
	        Naming.rebind("rmi://"+ADDRESS_STRING+":8888/teachPlanData", teachPlanData);
	        CourseDatabaseService courseData=databaseFactory.getCourseDatabaseService();
	        Naming.rebind("rmi://"+ADDRESS_STRING+":8888/courseData", courseData);
	        ChooseCourseDatabaseService chooseCourseData=databaseFactory.getChooseCourseDatabaseService();
	        Naming.rebind("rmi://"+ADDRESS_STRING+":8888/chooseCourseData", chooseCourseData);
	        UserDatabaseService userData=databaseFactory.getUserDatabaseService();
	        Naming.rebind("rmi://"+ADDRESS_STRING+":8888/userData", userData);
	        FrameworkDatabaseService frameworkData=databaseFactory.getFrameworkDatabaseService();
	        Naming.rebind("rmi://"+ADDRESS_STRING+":8888/frameworkData", frameworkData);
	        InformDataService informData=databaseFactory.getInformDataService();
	        Naming.rebind("rmi://"+ADDRESS_STRING+":8888/informData", informData);
	        
	        System.out.println("服务器连接成功！");
	        System.out.println("------------------------------------------------------");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void closeRMI(){
		System.out.println("服务器已关闭！");
		System.exit(0);
		
	}

}
